package com.aaa.book_mall.controller;

import com.aaa.book_mall.dao.StaffDao;
import com.aaa.book_mall.entity.Staff;
import com.aaa.book_mall.util.LayTab;
import com.aaa.book_mall.util.Result;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName StarffController
 * @Description ToDo
 * @Auther teacher Bai
 * @Date 2020/12/26 10:43
 */
@RestController
@RequestMapping("/staff")
public class StarffController {

    @Autowired
    private StaffDao staffDao;

    @RequestMapping("/login")
    public Map login(String staffid, String password, HttpServletRequest request){
        //数据操作
        Staff staff = staffDao.getStaffByStaffIdAndPassword(staffid, password);
        HashMap<Object, Object> map = new HashMap<>();
        if(staff==null){
            map.put("code",300);
            map.put("msg","该用户不存在");
        }else {
            //先将staff存储session中
            request.getSession().setAttribute("staff",staff);
            map.put("code",200);
        }
        return map;
    }

    @RequestMapping("/exit")
    public void exit(HttpServletRequest request,HttpServletResponse response){
        HttpSession session = request.getSession();
        session.invalidate();
        System.out.println("exit");
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("login");
        try {
            response.sendRedirect("/login.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/getAllStaff")
    public LayTab getAllStaff(Staff staff){
        List<Map<String, Object>> allStaff = staffDao.getAllStaff(staff);
        int allStaffCount = staffDao.getStaffCount(staff);
        LayTab layTab = new LayTab(allStaffCount, allStaff);
        return layTab;
    }

    @RequestMapping("/insertStaff")
    //修改和添加数据
    public Map insertStaff(Staff staff) {
        System.out.println("---");
        System.out.println(staff);
        int i = 0;
        if (staff.getId()!= null || "".equals(staff.getId() )) {
            System.out.println("mao");
            i=staffDao.updateStaff(staff);
        } else {
            i =staffDao.insertStaff(staff);
            System.out.println("yun");
        }
        Map<Object, Object> map = new HashMap<>();
        if (i>0) {
            map.put("code","200");
            map.put("msg", "添加成功");
        }else {
            map.put("code","300");
            map.put("msg", "添加失败");
        }
        return map;
    }

    @RequestMapping("/insertLeaveStaff")
    //修改和添加数据
    public Map insertLeaveStaff(Staff staff) {
        System.out.println("---");
        int i = 0;
        if (staff.getId()!= null || "".equals(staff.getId() )) {
            System.out.println("mao");
            i=staffDao.updateLeaveStaff(staff);
        } else {
            i =staffDao.insertStaff(staff);
            System.out.println("yun");
        }
        Map<Object, Object> map = new HashMap<>();
        if (i>0) {
            map.put("code","200");
            map.put("msg", "添加成功");
        }else {
            map.put("code","300");
            map.put("msg", "添加失败");
        }
        return map;
    }

    @RequestMapping("/delById")
    protected Result delById(Integer id) {
        int i = staffDao.delById(id);

        Result result =null;
        if(i>0){
            result = new Result("1","删除成功");
        }else {
            result = new Result("5","删除失败");
        }
        return result;
    }

    @RequestMapping("/updateStatus")
    protected Map<Object, Object> updateStatus (Integer id, Integer status){
        int i = staffDao.updateStatus(id, status);
        Map<Object, Object> map = new HashMap<>();
        if (i>0) {
            map.put("code","200");
            map.put("msg", "添加成功");
        }else {
            map.put("code","300");
            map.put("msg", "添加失败");
        }
        return map;
    }




    /**
     * 使用Lish<Map>查询所有离职员工信息
     * @param staff
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/getStaff")
    protected LayTab getStaff(Staff staff)  {
        List<Map<String,Object>> staffs = staffDao.getStaff(staff);
        int count = staffDao.getStaffCount(staff);
        LayTab layTab= new LayTab(count,staffs);
        return layTab;
    }
    @RequestMapping("/getAllRole")
    protected void getAllRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //StaffDaoimpl dao = new StaffDaoimpl();
        List<Map<String, Object>> allRole = staffDao.getAllRole();
        String s = JSON.toJSONString(allRole);
        resp.getWriter().write(s);
    }
    @RequestMapping("/getAllDept")
    protected void getAllDept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //StaffDaoimpl dao = new StaffDaoimpl();
        List<Map<String, Object>> allDept = staffDao.getAllDept();
        String s = JSON.toJSONString(allDept);
        resp.getWriter().write(s);
    }
}
